<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>客户管理</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
    <h2 class="mb-4">客户信息管理</h2>

    <!-- 查询表单 -->
    <form class="form-inline mb-3" method="get" action="/admin/user/list">
        <input type="text" class="form-control mr-2" name="customerName" placeholder="客户姓名"
               th:value="${customerName}">
        <input type="text" class="form-control mr-2" name="PID" placeholder="身份证号"
               th:value="${PID}">
        <button type="submit" class="btn btn-primary mr-2">查询</button>
    </form>

    <div class="mb-3 text-right">
        <a href="/admin/customerAdd" class="btn btn-primary">添加客户</a>
        <button class="btn btn-success" onclick="exportCsv()">导出CSV</button>
    </div>


    <!-- 客户列表 -->
    <table class="table table-bordered table-hover">
        <thead class="thead-light">
        <tr>
            <th>客户编号</th>
            <th>姓名</th>
            <th>身份证号</th>
            <th>电话</th>
            <th>地址</th>
            <th>角色</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="user : ${userList}">
            <td th:text="${user.customerId}"></td>
            <td th:text="${user.name}"></td>
            <td th:text="${user.pid}"></td>
            <td th:text="${user.telephone}"></td>
            <td th:text="${user.address}"></td>
            <td th:text="${user.role == 'ADMIN' ? '管理员' : '客户'}"></td>
            <td>
                <a th:href="@{'/admin/user/edit/' + ${user.customerId}}" class="btn btn-sm btn-warning">修改</a>
                <a th:href="@{'/admin/user/delete/' + ${user.customerId}}"  onclick="return confirm('确认删除?')" class="btn btn-sm btn-danger">删除</a>
            </td>
        </tr>
        <tr th:if="${#lists.isEmpty(userList)}">
            <td colspan="7" class="text-center text-muted">暂无数据</td>
        </tr>
        </tbody>
    </table>

    <div class="text-center mt-4 mb-4">
        <a th:href="@{/admin/index}" class="btn btn-primary">返回首页</a >
    </div>


    <script>
        const token = localStorage.getItem("token");

        if (!token) {
            alert("请先登录！");
            window.location.href = "/login.html";
        }
        function exportCsv() {
            window.location.href = "/admin/user/export?token=" + token;
        }
    </script>
</div>
</body>
</html>